package com.hava.homework;

import com.hava.homework.entity.UserBalance;
import com.hava.homework.repository.UserBalanceJdbcTemplateDao;
import com.hava.homework.repository.UserBalanceMyBatisDao;
import com.hava.homework.service.TransferBalanceService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by yanfa on 2016/10/27.
 */
public class UserBalanceMain {

    public static void main(String [] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("homework-context.xml");

        // get dao bean
        UserBalanceJdbcTemplateDao userBalanceJdbcTemplateDao = context.getBean("userBalanceJdbcTemplateDao",UserBalanceJdbcTemplateDao.class);
        UserBalanceMyBatisDao userBalanceMyBatisDao = context.getBean("userBalanceMyBatisDao",UserBalanceMyBatisDao.class);

        // get service
        TransferBalanceService transferBalanceService = context.getBean("transferBalanceService",TransferBalanceService.class);

        System.out.println("transferBalanceService.setUserBalanceDao is userBalanceJdbcTemplateDao");
        transferBalanceService.setUserBalanceDao(userBalanceJdbcTemplateDao);
        for(UserBalance userBalance : transferBalanceService.findAll())
            System.out.println("userBalance[id]:" +userBalance.getUserId() + " [balance]:" + userBalance.getBalance());

        try {
            transferBalanceService.transferMoney(1l, 2l, 100l);
        }catch(Exception e)
        {
            System.out.println(e.toString());
        }

        for(UserBalance userBalance : transferBalanceService.findAll())
            System.out.println("userBalance[id]:" +userBalance.getUserId() + " [balance]:" + userBalance.getBalance());

        System.out.println("transferBalanceService.setUserBalanceDao is userBalanceMyBatisDao");
        transferBalanceService.setUserBalanceDao(userBalanceMyBatisDao);
        for(UserBalance userBalance : transferBalanceService.findAll())
            System.out.println("userBalance[id]:" +userBalance.getUserId() + " [balance]:" + userBalance.getBalance());

        try {
            transferBalanceService.transferMoney(2l,1l,100l);
        }catch(Exception e)
        {
            System.out.println(e.toString());
        }

        for(UserBalance userBalance : transferBalanceService.findAll())
            System.out.println("userBalance[id]:" +userBalance.getUserId() + " [balance]:" + userBalance.getBalance());

        ((ConfigurableApplicationContext) context).close();
    }
}
